//
//  TopCollectionViewCell.m
//  MountEverestFM
//
//  Created by ChenNing on 15/10/31.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "TopCollectionViewCell.h"
#import "TopTableViewCell.h"

@interface TopCollectionViewCell()<UITableViewDataSource,UITableViewDelegate>
//数据
@property(nonatomic,strong)NSMutableArray *dataArray;
//tableView
@property(nonatomic,strong)UITableView *topTableView;
@end

@implementation TopCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化tableview
        [self initTopTableView];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.topTableView.frame = self.bounds;
}

/**
 *  初始化findTabelView
 */
- (void)initTopTableView
{
    self.topTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    self.topTableView.backgroundColor = UIColorFromRGB(0xe7e7e7);
    self.topTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.topTableView.dataSource = self;
    self.topTableView.delegate = self;
    [self.contentView addSubview:_topTableView];
    
    [self.topTableView registerClass:[TopTableViewCell class] forCellReuseIdentifier:@"topTBCell"];
    
    // 发送榜单请求
    [self setupRefresh];
}


// UITableViewDataSource 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[self.dataArray objectAtIndex:section] objectForKey:@"list"] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return FM_Screen_Pts_Width*80+FM_Screen_Pts_Height*20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FM_Screen_Pts_Width*50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FM_Screen_Pts_Height*15;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Width*50)];
    titleView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*10, (FM_Screen_Pts_Width*50-FM_Screen_Pts_Width*15)/2, FM_Screen_Pts_Width*15, FM_Screen_Pts_Width*15)];
    titleImageView.image = [UIImage imageNamed:@"findsection_logo@2x.png"];
    [titleView addSubview:titleImageView];

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*30, 0, FM_Screen_Pts_Width*100, FM_Screen_Pts_Width*50)];
    titleLabel.text = [[self.dataArray objectAtIndex:section] objectForKey:@"title"];
    titleLabel.font = [UIFont systemFontOfSize:15];
    [titleView addSubview:titleLabel];
    
    return titleView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topTBCell"];
    NSDictionary *tempDic = [self.dataArray objectAtIndex:indexPath.section];
    cell.topDictionary = [[tempDic objectForKey:@"list"] objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *cellDic = [[[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"list"] objectAtIndex:indexPath.row];
    NSString *contentType = [cellDic objectForKey:@"contentType"];
    NSString *key = [cellDic objectForKey:@"key"];
    NSString *title = [cellDic objectForKey:@"title"];
    
    NSString *sectionTitle = [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"title"];
    if([sectionTitle isEqualToString:@"主播榜单"]){
        [self.delegate topPage:contentType key:key title:title type:@"主播"];
    }else{
        [self.delegate topPage:contentType key:key title:title type:@"节目"];
    }
}

/**
 *  网络请求
 */
- (void)getDataWithUrl
{
    self.dataArray = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=ios-b1&device=iPhone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.20"];
    [NetWorkManager GET:url isCaChe:YES resultData:^(id responseObject) {
        self.dataArray = [responseObject objectForKey:@"datas"];
        [self.topTableView reloadData];
        [self setupEndRefresh];
    } resultRrror:^(NSError *error) {
        NSLog(@"榜单:error,%@",error);
    }];
}

// 设置刷新
- (void)setupRefresh
{
    // 头
    self.topTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getDataWithUrl)];
    [self.topTableView.header beginRefreshing];
}

// 结束刷新
- (void)setupEndRefresh
{
    [self.topTableView.header endRefreshing];
}

@end
